/*
 * wifi.h
 *
 *  Created on: 2021年12月23日
 *      Author: soliber
 */

#ifndef BSP_E103_W08_WIFI_H_
#define BSP_E103_W08_WIFI_H_

//Include
#include "usart.h"

#include "atcmd.h"
#include "parser.h"
#include "packager.h"

#include "inet.h"
#include "sockets.h"

#include "usart.h"

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "cmsis_os2.h"
#include "FreeRTOS.h"
#include "stream_buffer.h"
#include "semphr.h"

#include "SEGGER_RTT.h"
#include "elog.h"


//Define
#define wifi_uart huart2

#define WIFI_MAX_SSID_LEN 15
#define WIFI_MAX_PSWD_LEN 15
#define WIFI_MIN_LINK_NUM 1
#define WIFI_MAX_LINK_NUM 4

#define NUM_SOCKETS 4
#define STR1(R)	#R
#define STR2(R) STR1(R)
#define NUM_SOCKETS_STR STR2(NUM_SOCKETS)

#define SOCKET_RX_BUF_SIZE 100
#define SOCKET_TX_BUF_SIZE 100


//Macros
#define min(a,b)	((a)<(b)?(a):(b))


//Typedef
typedef enum{
	WIFI_OP_STATUS_INIT = 0,
	WIFI_OP_STATUS_TRANS = 1,
	WIFI_OP_STATUS_AT = 2,
}WIFI_Op_Status_t;

typedef enum{
	WIFI_STATUS_OK = 0,
	WIFI_STATUS_REQ_DATA_STAGE = 1,
	WIFI_STATUS_ERROR = 2,
	WIFI_STATUS_TIMEOUT = 3,
	WIFI_STATUS_IO_ERROR = 4,
	WIFI_STATUS_UNEXPECTED_CLOSED_SOCKET = 5,
	//参考7.1错误码表
	WIFI_ERROR_CMD_NOT_FOUND = -1,
	WIFI_ERROR_PARAM_OUT_OF_RANGE = -2,
	WIFI_ERROR_MAC_OP_FAILED = -3,
	WIFI_ERROR_MANUAL_CONNECT_FAILED = -4,
	WIFI_ERROR_START_SCAN_FAILED = -5,
	WIFI_ERROR_STOP_SCAN_FAILED = -6,
	WIFI_ERROR_DISCONNECT_FAILED = -7,
	WIFI_ERROR_TCP_ALREADY_EXISTS = -8,
	WIFI_ERROR_TCP_NOT_CONNECT = -9,
	WIFI_ERROR_NO_CRLF = -11,
	WIFI_ERROR_MODE_UNSUPPORT = -20,
	WIFI_ERROR_READ_RSSI_FAILED = -21,
	WIFI_ERROR_READ_WRTIE_PARAM_FAILED = -30
}WIFI_Status_t;

typedef enum{
	WIFI_MODE_TCP = 0,
	WIFI_MODE_MQTT = 1,
	WIFI_MODE_HTTP = 2,
}WIFI_OperationMode_t;

typedef enum {
  WIFI_SEC_OPEN = 0x00,          /*!< Wifi is open */
  WIFI_SEC_WEP  = 0x01,          /*!< Wired Equivalent Privacy option for wifi security. \note This mode can't be used when setting up ES_WIFI wifi */
  WIFI_SEC_WPA  = 0x02,          /*!< Wi-Fi Protected Access */
  WIFI_SEC_WPA2 = 0x03,          /*!< Wi-Fi Protected Access 2 */
  WIFI_SEC_WPA_WPA2 = 0x04,      /*!< Wi-Fi Protected Access with both modes */
  WIFI_SEC_ENTERPRISE = 0x05,
}WIFI_SecurityType_t;

typedef enum{
	WIFI_AP_AUTO = 0,
	WIFI_AP_MANUAL = 1,
	WIFI_AP_BLE = 2,
}WIFI_ConnectMode_t;

typedef enum{
	WIFI_TCP_MANUAL_TRANSPARENT,
	WIFI_TCP_MANUAL_PROTOCOL,
	WIFI_TCP_AUTO_TRANSPARENT,
	WIFI_TCP_AUTO_PROTOCOL,
}WIFI_TransportMode_t;

typedef struct
{
	uint16_t	RemotePort;
	uint8_t		RemoteIP[4];
	uint16_t	LocalPort;
	uint8_t 	Number;
}WIFI_Transport_t;

typedef struct
{
	char ssid[WIFI_MAX_SSID_LEN+1];
	char pswd[WIFI_MAX_PSWD_LEN+1];
	WIFI_SecurityType_t Security;	//加密类型，参考7.3.2
	uint8_t MAC_Addr[6];			//MAC地址，出厂固定不可修改，参考7.3.5
	uint8_t IP_Addr[4];				//IP地址，由AP分配，参考7.4.4
}WIFI_Network_t;

typedef struct
{
	uint16_t FWVer;						//0x1234 = SW V1.2, HD V3.4
	WIFI_OperationMode_t OperationMode;	//工作模式：TCP/MQTT/HTTP，参考7.2.6
	WIFI_ConnectMode_t ConnectMode;		//连接模式：自动/手动/BEL配网，参考7.3.4
	WIFI_TransportMode_t TCPMode;		//TCP模式：手动/自动连接模式、透传/协议传输模式，参考7.4.6
	WIFI_Network_t NetSettings;			//网络参数
	uint8_t TransportNum;				//多连接socket数量，参考7.4.5
	WIFI_Transport_t Transport[WIFI_MAX_LINK_NUM];	//socket参数，最多4路socket，参考7.4.8
}WIFI_Object_t;

typedef struct wifi_socket
{
	osMutexId_t lock;			//锁
	StreamBufferHandle_t rxBuf;	//接收数据buffer
	StreamBufferHandle_t txBuf;	//发送数据buffer
	struct sockaddr_in remote;	//远程主机
	uint8_t fd;					//文件描述符

	//选项
	uint32_t connTimeout;
	uint32_t rxTimeout;
	uint32_t txTimeout;
	uint16_t rxBufSize;
	uint16_t txBufSize;
}wifi_socket_t;

//Function Prototypes
void WIFI_Init(void);
void WIFI_DeInit(void);

void WIFI_Lock(void);
void WIFI_Unlock(void);

wifi_socket_t* get_socket(int s);

#endif /* BSP_E103_W08_WIFI_H_ */
